<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- <script src="./2.js"></script> -->
    <input type="text" id="searchInput">
    <span id="searcherSpan"></span>
    <script>
        // 双向绑定
        const searchInput = document.getElementById('searchInput');
        const searcherSpan = document.getElementById('searcherSpan');
        const obj = {value:''}; // 被代理的对象
        // Proxy可以对某个对象的所有属性监听
        // const proxyObj = new Proxy(obj, {
        //     set:function(target,key,val) {
        //         searcherSpan.innerHTML = val;
        //         target[key] = val;
        //     }
        // })

        // 'value'对obj中value属性监听
        Object.defineProperty(obj,'value', {
            set:function(newVal){
                console.log('set val'+newVal);
                searchInput.value=newVal;
                searcherSpan.innerText=newVal;
            }
        })

        searchInput.addEventListener('keyup',function(e) {
            // proxyObj.value=e.target.value;
            obj.value=e.target.value;
        })
    </script>
</body>
</html>